<template>
	<view class="setting">
		<gyd-navbar back-color="#fff" :title="title"></gyd-navbar>

		<up-cell-group>
			<up-cell :title="member.nickname" :label="member.mobile" :isLink="true" url="/userPages/setting/personal">
				<template #icon>
					<u-avatar :src="member.avatar" size="80" style="margin-right: 16rpx;"></u-avatar>
				</template>
			</up-cell>
		</up-cell-group>

		<up-cell-group>
			<up-cell title="修改登录密码" :isLink="true" url="/userPages/setting/change-password"></up-cell>
			<up-cell title="修改支付密码" :isLink="true" url="/userPages/setting/change-pay-password"></up-cell>
			<up-cell title="提现账户" :isLink="true" url="/userPages/wallet/account"></up-cell>
		</up-cell-group>

		<!-- #ifdef APP -->
		<up-cell-group>
			<up-cell title="清除缓存" :value="fileSizeString" :isLink="true" @click="clearCache()"></up-cell>
			<up-cell title="版本检查" :value="'V'+version" :isLink="true" @click="checkVersion()"></up-cell>
		</up-cell-group>
		<!-- #endif -->

		<up-cell-group>
			<up-cell title="消息通知" isLink url="/servePages/notice/notice">
				<template #value>
					<up-badge type="info" max="99" :value="noticeNum"></up-badge>
				</template>
			</up-cell>
			<up-cell title="注销账户" :isLink="true" @click="closeAccount"></up-cell>
		</up-cell-group>

		<up-button class="exit" type="info" shape="circle" :plain="true" :hairline="true" text="退出登录"
			@click="loginOut()"></up-button>

		<gyd-upgrade-app ref="upgradeAppRef"></gyd-upgrade-app>
	</view>
</template>

<script setup>
	import request from '@/utils/request';
	import cache from '@/utils/cache';
	import util from '@/utils/util';
	import {
		reactive,
		toRefs,
		inject,
		ref,
	} from 'vue';
	import {
		onLoad,
		onShow,
		onReady,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app';

	const state = reactive({
		title: '系统设置',
		themeColor: cache.get('themeColor'),
		imgHost: util.requestUrl(),
		member: false,
		noticeNum: '123',
	});

	const fileSizeString = ref('0Kb'); //缓存大小
	const version = ref('1.0.0'); //APP版本
	const upgradeAppRef = ref(null);

	//在页面所有元素加载完成后执行（只执行一次）
	onLoad((opt) => {
		console.log('onLoad');
		if (opt.hasOwnProperty('id')) {
			console.log('传递参数', opt.id);
		}
		initData();
	});

	//在页面由不可见变为可见时执行（只要页面显示都会执行）
	onShow(() => {
		console.log('onShow');
	})

	//在页面解析完成之后执行
	onReady(() => {
		console.log('onReady');
	})

	onPullDownRefresh(() => {
		console.log('下拉刷新');
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 1000);
	})

	onReachBottom(() => {
		console.log('触底加载更多');
	})

	function initData() {
		state.member = {
			id: 10001,
			avatar: state.imgHost + '/static/images/avatar.png',
			nickname: '王云海',
			mobile: '15825258992',
			tags: '',
			agent_level: 1,
			agent_level_name: '省级代理',

			money: '100.00',
			income: '115.00',
			commission: '2100.35',
			coupon: 2,
			points: '130.00',
			water_tickets: 10,
			opening_award: '16.50',
			create_time: '2025-08-18 13:26:16'
		}

		// #ifdef APP
		getCache();
		getVertion();
		// #endif
	}

	//获取缓存
	function getCache() {
		plus.cache.calculate(function(size) {
			let sizeCache = parseInt(size);
			if (sizeCache == 0) {
				fileSizeString.value = '0B';
			} else if (sizeCache < 1024) {
				fileSizeString.value = sizeCache + 'B';
			} else if (sizeCache < 1048576) {
				fileSizeString.value = (sizeCache / 1024).toFixed(2) + 'KB';
			} else if (sizeCache < 1073741824) {
				fileSizeString.value = (sizeCache / 1048576).toFixed(2) + 'MB';
			} else {
				fileSizeString.value = (sizeCache / 1073741824).toFixed(2) + 'GB';
			}
		})
	}

	//清除缓存
	function clearCache() {
		plus.cache.calculate(function(size) {
			//使用 plus.cache.clear 清除应用中的缓存数据 这里清除后还要二十几KB没有清除，达不到全部清除
			plus.cache.clear(function() {
				uni.showToast({
					title: '缓存清除成功',
					icon: 'none',
					success() {
						//成功后处理
						getCache();
					}
				})
			});
		})
	}

	//获取当前安装版本
	function getVertion() {
		uni.getSystemInfo({
			success: (res) => {
				console.log('当前安装版本', res);
				version.value = res.appVersion;
			}
		})
	}

	//检查版本
	function checkVersion() {
		// #ifdef H5 || MP
		util.showTip('已经是最新版本');
		// #endif

		// #ifdef APP
		uni.getSystemInfo({
			success: (data) => {
				console.log('系统信息', data);
				request.get('index/getAppVersion', {
					version: version.value,
					appType: 'user',
					platformType: data.platform
				}).then(res => {
					console.log('获取更新', res);
					if (res.code == 1) {
						if (res.data.isUpdate) {
							console.log('需要更新', res.data.isUpdate);
							upgradeAppRef.value.open();
						} else {
							console.log('不需要更新', res.data.isUpdate);
							uni.showToast({
								title: '已经是最新版本',
								icon: 'none'
							})
						}
					}
				})
			}
		})
		// #endif
	}

	function closeAccount() {
		uni.showModal({
			title: '温馨提示',
			content: '注销账户将失去在本平台所有权益，是否继续注销？',
			success: function(res) {
				if (res.confirm) {
					console.log('用户点击确定');
					pageRoute('/userPages/setting/drop-account');
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}

	function loginOut() {
		console.log('退出登录');
	}

	function pageRoute(url) {
		uni.navigateTo({
			url
		});
	}

	function pageTabRoute(url) {
		uni.reLaunch({
			url
		});
	}

	const {
		title,
		themeColor,
		imgHost,
		member,
		noticeNum,
	} = toRefs(state);
</script>

<style lang="scss">
	page {
		background-color: $u-bg-color;
	}

	.setting {
		.exit {
			margin-top: 50rpx;
		}
	}
</style>